﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BrokenMind.Units
{
    class Player
    {
        private Rectangle rect;

        private Texture2D idle;
        private Texture2D runG;
        private Texture2D runUp;
        private Texture2D runDown;

        private int frameWidth;
        private int frameHeight;

        public int Frames
        {
            get { return runG.Width/frameWidth; }
            
        }

        private int currentFrame;
        private int timeElapsed;
        private int timeForFrame = 100;

        private bool isRunning;
        private bool isRunningRight;
        private bool isRunningUp;
        private bool isRunningDown;

        private bool isRunningUpLeft;
        private bool isRunningUpRight;
        private bool isRunningDownLeft;
        private bool isRunningDownRight;

        private Game1 game;

        public Player(Rectangle rect, Texture2D idle, Texture2D runG, Texture2D runUp, Texture2D runDown, Game1 game)
        {
            this.rect = rect;
            this.idle = idle;
            this.runG = runG;
            this.runUp = runUp;
            this.runDown = runDown;

            
            frameWidth = frameHeight = runG.Height;
            this.game = game;

        }

        public void StartRunG(bool isRight)
        {
            isRunning = true;

            isRunningDown = false;
            isRunningUp = false;
            isRunningUpLeft = false;
            isRunningUpRight = false;
            isRunningDownLeft = false;
            isRunningDownRight = false;
            isRunningRight = isRight;
        }

        public void StartRunUp()
        {
            isRunningUp = true;

            isRunning = false;
            isRunningDown = false;
            isRunningUpLeft = false;
            isRunningUpRight = false;
            isRunningDownLeft = false;
            isRunningDownRight = false;
        }
        
        public void StartRunDown()
        {
            isRunningDown = true;

            isRunning = false;
            isRunningUp = false;
            isRunningUpLeft = false;
            isRunningUpRight = false;
            isRunningDownLeft = false;
            isRunningDownRight = false;
        }

        public void StartRunUpLeft()
        {
            isRunningUpLeft = true;

            isRunning = false;
            isRunningDown = false;
            isRunningUp = false;
            isRunningUpRight = false;
            isRunningDownLeft = false;
            isRunningDownRight = false;
        }

        public void StartRunUpRight()
        {
            isRunningUpRight = true;

            isRunning = false;
            isRunningDown = false;
            isRunningUp = false;
            isRunningUpLeft = false;
            isRunningDownLeft = false;
            isRunningDownRight = false;
        }

        public void StartRunDownLeft()
        {
            isRunningDownLeft = true;

            isRunning = false;
            isRunningDown = false;
            isRunningUp = false;
            isRunningUpLeft = false;
            isRunningUpRight = false;
            isRunningDownRight = false;
        }

        public void StartRunDownRight()
        {
            isRunningDownRight = true;

            isRunning = false;
            isRunningDown = false;
            isRunningUp = false;
            isRunningUpLeft = false;
            isRunningUpRight = false;
            isRunningDownLeft = false;
        }

        public void Stop()
        {
            isRunning = false;
            isRunningDown = false;
            isRunningUp = false;
            isRunningUpLeft = false;
            isRunningUpRight = false;
            isRunningDownLeft = false;
            isRunningDownRight = false;
            currentFrame = 0;
            timeElapsed = 0;
        }

        public void Update(GameTime gameTime)
        {
            if (isRunning)
            {
                timeElapsed += gameTime.ElapsedGameTime.Milliseconds;
                if(timeElapsed > timeForFrame)
                {
                    currentFrame = (currentFrame + 1)%Frames;
                    timeElapsed = 0;
                }

                int moveX = 3*gameTime.ElapsedGameTime.Milliseconds / 10;
                

                if(!isRunningRight)
                {
                    moveX = -moveX;
                }
                

                Rectangle nextPositionX = rect;
                nextPositionX.Offset(moveX,0);

                if(nextPositionX.Left > 0 && nextPositionX.Right < game.Width)
                {
                    rect = nextPositionX;
                }
                
            }
            else if(isRunningUp || isRunningDown)
            {
                timeElapsed += gameTime.ElapsedGameTime.Milliseconds;
                if (timeElapsed > timeForFrame)
                {
                    currentFrame = (currentFrame + 1) % Frames;
                    timeElapsed = 0;
                }

                int moveY = 3 * gameTime.ElapsedGameTime.Milliseconds / 10;

                if (!isRunningDown)
                {
                    moveY = -moveY;
                }

                Rectangle nextPositionY = rect;
                nextPositionY.Offset(0, moveY);

                if (nextPositionY.Top > 0 && nextPositionY.Bottom < game.Height)
                {
                    rect = nextPositionY;
                }
            }
            else if(isRunningUpLeft)
            {
                timeElapsed += gameTime.ElapsedGameTime.Milliseconds;
                if (timeElapsed > timeForFrame)
                {
                    currentFrame = (currentFrame + 1) % Frames;
                    timeElapsed = 0;
                }

                int moveY = 3 * gameTime.ElapsedGameTime.Milliseconds / 10;
                int moveX = 3 * gameTime.ElapsedGameTime.Milliseconds/10;

                Rectangle nextPositionXY = rect;
                nextPositionXY.Offset(-moveX,-moveY);

                if ((nextPositionXY.Top > 0 && nextPositionXY.Bottom < game.Height) && (nextPositionXY.Left > 0 && nextPositionXY.Right < game.Width))
                {
                    rect = nextPositionXY;
                }
            }
            else if (isRunningDownLeft)
            {
                timeElapsed += gameTime.ElapsedGameTime.Milliseconds;
                if (timeElapsed > timeForFrame)
                {
                    currentFrame = (currentFrame + 1) % Frames;
                    timeElapsed = 0;
                }

                int moveY = 3 * gameTime.ElapsedGameTime.Milliseconds / 10;
                int moveX = 3 * gameTime.ElapsedGameTime.Milliseconds / 10;

                Rectangle nextPositionXY = rect;
                nextPositionXY.Offset(-moveX, moveY);

                if ((nextPositionXY.Top > 0 && nextPositionXY.Bottom < game.Height) && (nextPositionXY.Left > 0 && nextPositionXY.Right < game.Width))
                {
                    rect = nextPositionXY;
                }
            }
            else if (isRunningUpRight)
            {
                timeElapsed += gameTime.ElapsedGameTime.Milliseconds;
                if (timeElapsed > timeForFrame)
                {
                    currentFrame = (currentFrame + 1) % Frames;
                    timeElapsed = 0;
                }

                int moveY = 3 * gameTime.ElapsedGameTime.Milliseconds / 10;
                int moveX = 3 * gameTime.ElapsedGameTime.Milliseconds / 10;

                Rectangle nextPositionXY = rect;
                nextPositionXY.Offset(moveX, -moveY);

                if ((nextPositionXY.Top > 0 && nextPositionXY.Bottom < game.Height) && (nextPositionXY.Left > 0 && nextPositionXY.Right < game.Width))
                {
                    rect = nextPositionXY;
                }
            }
            else if (isRunningDownRight)
            {
                timeElapsed += gameTime.ElapsedGameTime.Milliseconds;
                if (timeElapsed > timeForFrame)
                {
                    currentFrame = (currentFrame + 1) % Frames;
                    timeElapsed = 0;
                }

                int moveY = 3 * gameTime.ElapsedGameTime.Milliseconds / 10;
                int moveX = 3 * gameTime.ElapsedGameTime.Milliseconds / 10;

                Rectangle nextPositionXY = rect;
                nextPositionXY.Offset(moveX, moveY);

                if ((nextPositionXY.Top > 0 && nextPositionXY.Bottom < game.Height) && (nextPositionXY.Left > 0 && nextPositionXY.Right < game.Width))
                {
                    rect = nextPositionXY;
                }
            }


        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();

            if(isRunning)
            {
                Rectangle currentRect = new Rectangle(currentFrame*frameWidth, 0, frameWidth, frameHeight);
                SpriteEffects flipEffects = SpriteEffects.None;
                if(isRunningRight)
                {
                    flipEffects = SpriteEffects.FlipHorizontally;
                }
             
                spriteBatch.Draw(runG,rect,currentRect,Color.White,0,Vector2.Zero,flipEffects,0);
            }
            else if(isRunningUp)
            {
                Rectangle currentRect = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);

                spriteBatch.Draw(runUp, rect, currentRect, Color.White);
            }
            else if (isRunningDown)
            {
                Rectangle currentRect = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);

                spriteBatch.Draw(runDown, rect, currentRect, Color.White);
            }
            else
            {
                spriteBatch.Draw(idle, rect, Color.White);
            }

            spriteBatch.End();
        }
    } 
}
